package com.zhiyou100.crm.servlet.customerState;

import java.io.IOException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhiyou100.crm.service.CustomerStateService;
import com.zhiyou100.crm.service.impl.CustomerStateServiceImpl;
import com.zhiyou100.crm.util.AdminBaseServlet;
import com.zhiyou100.crm.util.CustomerState;

/**
 * 更新客户状态
 */
@WebServlet("/customerState/update")
public class UpdateServlet extends AdminBaseServlet {
	private static final long serialVersionUID = 1L;
	CustomerStateService customerStateService = new CustomerStateServiceImpl();


	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		int id = Integer.parseInt(request.getParameter("id"));
		List<CustomerState> customerStates = customerStateService.query();
		if(customerStates!=null){
			for (int i = 0; i < customerStates.size(); i++) {
				int customer_state_id = customerStates.get(i).getCustomer_state_id();
				if(customer_state_id==id){
					request.setAttribute("customerState",customerStates.get(i));
					break;
				}
			}
			request.getRequestDispatcher("/WEB-INF/view/customerState/update.jsp").forward(request,response);
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		CustomerState customerState=new CustomerState();
		customerState.setCustomer_state_id(Integer.parseInt(request.getParameter("customerStateId")));
		customerState.setCustomer_state_name(request.getParameter("customerStateName"));
		customerState.setCustomer_state_desc(request.getParameter("customerStateDesc"));
		int a = customerStateService.update(customerState);
		if(a>0) {
			response.sendRedirect(response.getContentType()+"/customerState/list.jsp");
		}else {
			request.getRequestDispatcher("/WEB-INF/view/customerState/update.jsp").forward(request, response);
		}
	}

}
